package trabajoISW2.domain.implementations;

import java.util.Calendar;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import trabajoISW2.domain.contracts.IBook;
import trabajoISW2.domain.exceptions.NoPositiveNumberException;
import trabajoISW2.domain.exceptions.WrongNameException;

@Entity
public class Book implements IBook {

	@Id
	private String isbn;
	private String title;
	private double price;
	private String summary;
	private String language;
	private int year;

	@ManyToOne (cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
	private Author author;

	@ManyToOne (cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
	private Publisher publisher;

	@ManyToOne (cascade = {CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REFRESH})
	private Subject subject;

	// Constructor
	public Book(String isbn, String title, double price, String summary,
			String language, int year, Author author, Subject subject,
			Publisher publisher) {
		this.isbn = isbn;
		this.title = title;
		this.price = price;
		this.summary = summary;
		this.price = price;
		this.language = language;
		this.year = year;
		this.author = author;
		this.publisher = publisher;
		this.subject = subject;

	}

	public Book() {
	}

	@Override
	public void setIsbn(String isbn) {
		if (isbn == null || (isbn.length() != 10 && isbn.length() != 13))
			throw new IllegalArgumentException();
		else
			this.isbn = isbn;
	}

	@Override
	public String getIsbn() {
		return this.isbn;
	}

	@Override
	public void setTitle(String title) {
		if (title == null | title == "")
			throw new WrongNameException();
		else
			this.title = title;
	}

	@Override
	public String getTitle() {
		return this.title;
	}

	@Override
	public void setPrice(double price) {
		if (price < 0 | price > Double.MAX_VALUE)
			throw new NoPositiveNumberException();
		else
			this.price = price;
	}

	@Override
	public double getPrice() {
		return this.price;
	}

	@Override
	public void setSummary(String summary) {
		if (summary == null | summary == "")
			throw new WrongNameException();
		else
			this.summary = summary;
	}

	@Override
	public String getSummary() {
		return this.summary;
	}

	@Override
	public void setLanguage(String language) {
		if (language == null | language == "")
			throw new WrongNameException();
		else
			this.language = language;
	}

	@Override
	public String getLanguage() {
		return this.language;
	}

	@Override
	public void setYear(int year) {
		if (year <= 0 | year > Calendar.YEAR)
			throw new NoPositiveNumberException();
		else
			this.year = year;
	}

	@Override
	public int getYear() {
		return this.year;
	}

	@Override
	public void setAuthor(Author author) {
		if (author == null)
			throw new IllegalArgumentException();
		else
			this.author = author;
	}

	@Override
	public Author getAuthor() {
		return this.author;
	}

	@Override
	public void setPublisher(Publisher publisher) {
		if (publisher == null)
			throw new IllegalArgumentException();
		else
			this.publisher = publisher;
	}

	@Override
	public Publisher getPublisher() {
		return this.publisher;
	}

	@Override
	public void setSubject(Subject subject) {
		if (subject == null)
			throw new IllegalArgumentException();
		else
			this.subject = subject;
	}

	@Override
	public Subject getSubject() {
		return this.subject;
	}


	
	public boolean equals (Object o){
		boolean b = false;
		Book bo = (Book) o;
		if (getIsbn().equals(bo.getIsbn()))
			b = true;
		return b;
	}
	
	public String toString (){
		return new String("ISBN :"+getIsbn()+"\nTitle: "+getTitle());
	}

}
